package com.xujie.chat.server.handler;

import com.xujie.chat.kryocodec.KryoDecoder;
import com.xujie.chat.kryocodec.KryoEncoder;
import com.xujie.chat.message.MyMessage;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.socket.SocketChannel;
import io.netty.handler.codec.LengthFieldBasedFrameDecoder;
import io.netty.handler.codec.LengthFieldPrepender;
import io.netty.handler.timeout.ReadTimeoutHandler;

/**
 * @author Xujie
 * @since 2024/12/28 18:07
 **/


public class ServerInit extends ChannelInitializer<SocketChannel> {
    @Override
    protected void initChannel(SocketChannel ch) throws Exception {
        ChannelPipeline pipeline = ch.pipeline();
        pipeline
//						.addLast(new LoggingHandler(LogLevel.INFO))
                .addLast(new LengthFieldBasedFrameDecoder(65535, 0, 2, 0, 2))
                .addLast(new LengthFieldPrepender(2))
                .addLast(new KryoDecoder<>(MyMessage.class))
                .addLast(new KryoEncoder())

                .addLast(new ReadTimeoutHandler(60))
                .addLast(new AuthHandler())
                .addLast(new HeartBeatHandler())
                .addLast(new MessageFilterHandler())
                .addLast(new MessageHandler())
                .addLast(new ExceptionHandler());

    }
}
